// 封装uni.request api
// const url_all: any = {
// 	'DEV': 'http://192.168.1.167:81', // 开发
// 	'PRO': 'http://121.43.177.20:8181', // 生产
// }
// let BASEURL = url_all['DEV'] // 调整当前环境
import { CONSTANT_URL } from '@/apis/constant'
let BASEURL = CONSTANT_URL
console.log(BASEURL)
/*
* 全局请求封装
* @param filePath 图片路径
* @param loading 请求未完成是是否显示加载中，默认为true
* @param showErrToast 请求失败是否显示错误提示，默认为true
* @param any 请求多图上传，默认为false
*/
export default (filePath: any, loading = true, showErrToast = true, any = false,) => {
	// 获取存储token
	const token = uni.getStorageSync("token");

	if (loading) {
		uni.showLoading({
			title: "加载中",
			mask: true
		});
	};
    let url = 'http://60.191.72.66:10089/business/api/file/fileUploadBase64'
    if (any) {
        url = 'http://60.191.72.66:10089/business/api/file/fileUploadBase64'
    }
    // #ifndef WEB
    // url = BASEURL + url
    // #endif
    if (any) {
        return new Promise((resolve, reject) => {
            uni.uploadFile({
                url,
                files: filePath,
                header: {
                    token
                },
                success(uploadFileRes) {
                    if (loading) {
                        uni.hideLoading()
                    }
                    // console.log(JSON.parse(uploadFileRes.data));
                    resolve(JSON.parse(uploadFileRes.data));
                },
                fail(err) {
                    if (loading) {
                        uni.hideLoading()
                    }
                    if (showErrToast) {
                        uni.showToast({
                        	icon: "error",
                            mask: true,
                        	title: '服务响应失败'
                        });
                    }
                    reject(err);
                },
            })
        })
    } else {
        return new Promise((resolve, reject) => {
            uni.uploadFile({
                url,
                filePath,
                name: 'file',
                header: {
                    token
                },
                formData: {
                    test: 1
                },
                success(uploadFileRes) {
                    if (loading) {
                        uni.hideLoading()
                    }
                    // console.log(JSON.parse(uploadFileRes.data));
                    resolve(JSON.parse(uploadFileRes.data));
                },
                fail(err) {
                    if (loading) {
                        uni.hideLoading()
                    }
                    if (showErrToast) {
                        uni.showToast({
                        	icon: "error",
                            mask: true,
                        	title: '服务响应失败'
                        });
                    }
                    reject(err);
                },
            })
        })
    }
    
};